<?php
// misc
function dbsize() {
  return round(filesize("database.sqlite")/1024);
}
function getlink($ns,$value,$text='none') {
  if($text == "none") {
    $text = $value;
  }
  return "<a href='".getURL($ns,$value)."'>$text</a>";
}
function getURL($ns,$value) {
  if($GLOBALS['config']['mod_rewrite_enabled'] == "true") {
    return $GLOBALS['config']['url']."$ns/$value";
  } else {
    return $GLOBALS['config']['url']."?$ns=$value";
  }
}
// theme helpers
function textilize($text) {
  include_once("Textile.php");
  $textile = new Textile;
  return $textile->process($text);
}
/* this baby is important. improvements needed:
* check if pages exist, make 'em look different
*/
function parselinks($input) {
  $final = "";
  $split = split("}",$input);
  for($i=0; $i<count($split)-1; $i++) {
    $segment = $split[$i];
    $split2 = split("{",$segment);
    $final .= $split2[0];
    $linkcontents = $split2[1];
    $split3 = split(":",$linkcontents);
    if(count($split3) == 1) {
      $linkns = "show";
    } else {
      $linkns = $split[0];
    }
    $final .= getLink($linkns,$linkcontents);
  }
  $final .= $split[count($split)-1];
  return $final;
}
function listTags($tagstring) {
  $tags = array();
  foreach(split(" ",$tagstring) as $tag) {
    array_push($tags,getLink("tag",$tag));
  }
  echo join($tags,", ");
}
function showParent($parentname) {
  if($parentname == "none") {
    echo "none";
  } else {
    echo getLink("show",$parentname);
  }
}
function listLinkedToBy($pagelist,$style="inline") {
  if(count($pagelist) > 0) {
    $links = array();
    foreach($pagelist as $page) {
      array_push($links,getLink("show",$page));
    }
    if($style == "inline") {
      echo join($links,", ");
    } else if($style == "list") {
      echo "<ul>\n";
      foreach($links as $link) {
        echo "<li>$link</li>\n";
      }
    }
  } else {
    echo "none";
  }
}
function showPageOutline($parent,$level=100000) {
  echo "<ul>\n";
  $pages = sqlite_fetch_all(sqlite_query($GLOBALS['db'],"SELECT * FROM pages ORDER BY name"));
  foreach($pages as $page) {
    $title = $page['name'];
    if($page['parent'] == $parent) {
      echo "<li>".getLink("show",$title)."</li>\n";
      if(!$level == 0) {
        showpageOutline($title,$level-1);
      }
    }
  }
  echo "</ul>\n";
}
function pathToSkin() {
  return $GLOBALS['config']['url']."skins/".$GLOBALS['config']['skin']."/";
}
?>